/**************************************************************************/
/*  EOSP                                                                  */
/*                                                                        */
/*  Copyright (c) 2013-2019                                               */
/*  All rights reserved.                                                  */
/*                                                                        */
/*  This project was developed by the SJQU Computer Department as part    */
/*  of the course 'Embedded Operation System Practice'.                   */
/*                                                                        */
/*  Redistribution and use in source and binary forms, with or without    */
/*  modification, are permitted provided that the following conditions    */
/*  are met:                                                              */
/*  1. Redistributions of source code must retain the above copyright     */
/*     notice, this list of conditions and the following disclaimer.      */
/*  2. Redistributions in binary form must reproduce the above copyright  */
/*     notice, this list of conditions and the following disclaimer in    */
/*     the documentation and/or other materials provided with the         */
/*     distribution.                                                      */
/*                                                                        */
/*  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''    */
/*  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED     */
/*  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A       */
/*  PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR   */
/*  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,          */
/*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES /INCLUDING, BUT NOT      */
/*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF      */
/*  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION/ HOWEVER CAUSED AND   */
/*  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,    */
/*  OR TORT /INCLUDING NEGLIGENCE OR OTHERWISE/ ARISING IN ANY WAY OUT    */
/*  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF    */
/*  SUCH DAMAGE.                                                          */
/**************************************************************************/
#include "includes.h"

static char rx_buff[DATABUF_DEFAULT_SIZE];

static void msg_handling()
{
  app_msg_pt msg = (app_msg_pt)rx_buff;
  uint8_t led_ctrl = 0;
  switch (msg->id) {
    case APP_LED_MSG_ID:
      led_ctrl = rx_buff[sizeof(*msg)];
      if (led_ctrl == LED_RGB_OFF) {
        LED_RGBOFF;
      } else if (led_ctrl == LED_RED_ON) {
        LED_RED;
      } else if (led_ctrl == LED_GREEN_ON) {
        LED_GREEN;
      } else if (led_ctrl == LED_BLUE_ON) {
        LED_BLUE;
      }
      break;
    case APP_ADC_MSG_ID:
      app_wakeup_sensor_task();
      break;
  }
}

void dump_hex_bytes(const char *prefix, const char *buff, int len)
{
  int i, pl;
  char sendbuf[64] = {0};
  
  pl = strlen(prefix);
  if (pl + 3*len + 3 > 64) {
    return;
  }
  
  sprintf(sendbuf, "%s", prefix);
  for (i = 0; i < len; ++i) {
    sprintf(sendbuf + (pl + i*3), "%02X ", buff[i]);
  }
  dlog("%s", sendbuf);
}

//NOTE: if some error happen, send [0xAA] to clear the data
void AppUartTask(void *p_arg)
{
  OS_ERR err;
  CPU_TS ts;
  databuf_pt pt = (databuf_pt)AppCtx.uart_databuf;
  int recv_bytes = 0;
  
  while (1) {
    OSTaskSemPend((OS_TICK)0, (OS_OPT)OS_OPT_PEND_BLOCKING,
            (CPU_TS*)&ts, (OS_ERR*)&err);
    
    recv_bytes = pt->dbsize(pt);
    pt->read(pt, rx_buff, recv_bytes);
    dump_hex_bytes("<<< ", (char*)rx_buff, recv_bytes);
    
    
    //TODO: more complicated handling NEEDED!!
    app_msg_pt msg = (app_msg_pt)rx_buff;
    if (msg->head != APP_MSG_HEAD || msg->dtsize > pt->size)
      continue;
    if (recv_bytes < sizeof(*msg) + 2)
      continue;
    if (recv_bytes != sizeof(*msg) + msg->dtsize + 2)
      continue;
    
    msg_handling();
  }
}




